package com.hunt.service.impl;


import com.hunt.dao.HisStatisUsersOnlineMapper;
import com.hunt.model.entity.*;
import com.hunt.service.OnlineStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * @author ww
 * @className
 * @description
 * @date 2020/6/15
 */

@Service
@Transactional
public class OnlineStatisticsServiceImpl implements OnlineStatisticsService {
    
    @Autowired
    private HisStatisUsersOnlineMapper hisStatisUsersOnlineMapper;


    @Override
    public OnlineStatisticsSerialize getOnlineStatisticsData(String getType) {
        //近30天访问统计数
        if (null == getType || "1".equals(getType) || "".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getTmpOnlineLast30DayData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getRentOnlineLast30DayData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getFormalOnlineLast30DayData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getNormalOnlineLast30DayData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getVIPOnlineLast30DayData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.PAST_30_DAY_ONLINE_STATISTICS.getTitleText());
        //近12月访问统计数    
        }else if ("2".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getTmpOnlineLast12MonthData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getRentOnlineLast12MonthData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getFormalOnlineLast12MonthData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getNormalOnlineLast12MonthData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getVIPOnlineLast12MonthData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.PAST_12_MONTH_ONLINE_STATISTICS.getTitleText());
        //本年度每周统计表    
        }else if("3".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getThisYearWeeklyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getThisYearWeeklyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getThisYearWeeklyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getThisYearWeeklyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getThisYearWeeklyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.THIS_YEAR_WEEKLY_ONLINE_SATTISTICS.getTitleText());
        //本年度每月统计表    
        }else if("4".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getThisYearMonthlyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getThisYearMonthlyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getThisYearMonthlyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getThisYearMonthlyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getThisYearMonthlyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.THIS_YEAR_MONTHLY_ONLINE_STATISTICS.getTitleText());
        //本年度每季统计表       
        }else if("5".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getThisYearQuarterlyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getThisYearQuarterlyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getThisYearQuarterlyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getThisYearQuarterlyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getThisYearQuarterlyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.THIS_YEAR_QUARTERLY_ONLINE_STATISTICS.getTitleText());
            //去年度每周统计表    
        }else if("6".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getPastYearWeeklyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getPastYearWeeklyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getPastYearWeeklyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getPastYearWeeklyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getPastYearWeeklyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.PAST_YEAR_WEEKLY_ONLINE_STATISTICS.getTitleText());
            //去年度每月统计表    
        }else if("7".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getPastYearMonthlyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getPastYearMonthlyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getPastYearMonthlyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getPastYearMonthlyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getPastYearMonthlyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.PAST_YEAR_MONTHLY_ONLINE_STATISTICS.getTitleText());
            //去年度每季统计表       
        }else if("8".equals(getType)){
            List<OnlineStatisticsData> tmpList = hisStatisUsersOnlineMapper.getPastYearQuarterlyTmpData();
            List<OnlineStatisticsData> rentList = hisStatisUsersOnlineMapper.getPastYearQuarterlyRentData();
            List<OnlineStatisticsData> formalList = hisStatisUsersOnlineMapper.getPastYearQuarterlyFormalData();
            List<OnlineStatisticsData> normalList = hisStatisUsersOnlineMapper.getPastYearQuarterlyNormalData();
            List<OnlineStatisticsData> vipList = hisStatisUsersOnlineMapper.getPastYearQuarterlyVIPData();
            return packageOnlineStatisticsData(tmpList,rentList,formalList,normalList,vipList,OnlineStatisticsType.PAST_YEAR_QUARTERLY_ONLINE_STATISTICS.getTitleText());
        }
        return null;
    }


    /**
     * 封装Highcharts数据格式
     * @param tmpList
     * @param rentList
     * @param formalList
     * @param titleText
     * @return
     */
    private OnlineStatisticsSerialize packageOnlineStatisticsData(List<OnlineStatisticsData> tmpList,
                                                                  List<OnlineStatisticsData> rentList,
                                                                  List<OnlineStatisticsData> formalList,
                                                                  List<OnlineStatisticsData> normalList,
                                                                  List<OnlineStatisticsData> vipList,
                                                                  String titleText){
        List<Integer> tmpData = new ArrayList<>();
        Iterator<OnlineStatisticsData> tmpIt = tmpList.iterator();
        while (tmpIt.hasNext()){
            tmpData.add(tmpIt.next().getNumbers());
        }

        List<Integer> rentData = new ArrayList<>();
        Iterator<OnlineStatisticsData> rentIt = rentList.iterator();
        while (rentIt.hasNext()){
            rentData.add(rentIt.next().getNumbers());
        }

        List<Integer> formalData = new ArrayList<>();
        Iterator<OnlineStatisticsData> formalIt = formalList.iterator();
        while (formalIt.hasNext()){
            formalData.add(formalIt.next().getNumbers());
        }

        List<Integer> normalData = new ArrayList<>();
        Iterator<OnlineStatisticsData> normalIt = normalList.iterator();
        while (normalIt.hasNext()){
            normalData.add(normalIt.next().getNumbers());
        }

        List<Integer> vipData = new ArrayList<>();
        Iterator<OnlineStatisticsData> vipIt = vipList.iterator();
        while (vipIt.hasNext()){
            vipData.add(vipIt.next().getNumbers());
        }

        List<String> timePointList = new ArrayList<>();
        for (OnlineStatisticsData onlineStatisticsData : tmpList) {
            timePointList.add(onlineStatisticsData.getDate());
        }
        OnlineStatisticsGroup tmpOnlineStatisticsGroup = new OnlineStatisticsGroup("体验用户",tmpData);
        OnlineStatisticsGroup rentOnlineStatisticsGroup  = new OnlineStatisticsGroup("租赁用户",rentData);
        OnlineStatisticsGroup formalOnlineStatisticsGroup = new OnlineStatisticsGroup("经典用户",formalData);
        OnlineStatisticsGroup normalOnlineStatisticsGroup = new OnlineStatisticsGroup("标准用户",normalData);
        OnlineStatisticsGroup vipOnlineStatisticsGroup = new OnlineStatisticsGroup("尊享用户",vipData);

        List<OnlineStatisticsGroup> onlineStatisticsGroupList = new ArrayList<>();
        onlineStatisticsGroupList.add(tmpOnlineStatisticsGroup);
        onlineStatisticsGroupList.add(rentOnlineStatisticsGroup);
        onlineStatisticsGroupList.add(formalOnlineStatisticsGroup);
        onlineStatisticsGroupList.add(normalOnlineStatisticsGroup);
        onlineStatisticsGroupList.add(vipOnlineStatisticsGroup);

        OnlineStatisticsSerialize onlineStatisticsSerialize = new OnlineStatisticsSerialize();
        onlineStatisticsSerialize.setTitleText(titleText);
        onlineStatisticsSerialize.setTimePointList(timePointList);
        onlineStatisticsSerialize.setOnlineStatisticsGroupList(onlineStatisticsGroupList);
        return onlineStatisticsSerialize;
    }
    
}
